var app = getApp(), number = null, weight = null, totalPrice = 0, express_price_1 = 0, express_price_2 = 0, v_privce = 0;

Page({
    data: {
        staus: 1,
        money: 0,
        express_price: 0,
        pay_price: 0,
        isexpress: !0,
        express_price_1: 0,
        express_price_2: 0,
        express1_isexpress: !0,
        address: null,
        formid: [],
        navIndex: 1,
        clubList: null,
        page: 1,
        pagesize: 20,
        loadend: !1,
        showClubList: !1,
        club: null,
        isaddr: !1,
        Clubloading: !1
    },
    navIndex: function(a) {
        var e = this, t = a.currentTarget.dataset.index;
        if (this.setData({
            navIndex: a.currentTarget.dataset.index
        }), 2 == t) null == this.data.club ? wx.getLocation({
            success: function(a) {
                e.setData({
                    longitude: a.longitude,
                    latitude: a.latitude
                }), app.util.request({
                    url: "entry/wxapp/community",
                    showLoading: !0,
                    data: {
                        op: "clubList",
                        longitude: a.longitude,
                        latitude: a.latitude,
                        page: e.data.page,
                        pagesize: e.data.pagesize
                    },
                    success: function(a) {
                        for (var t = 0; t < a.data.data.clubList.length; t++) 1e3 < a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = (a.data.data.clubList[t].juli / 1e3).toFixed(1) + "km" : a.data.data.clubList[t].julishow = a.data.data.clubList[t].juli + "m";
                        e.setData({
                            clubList: a.data.data.clubList,
                            club: a.data.data.clubList[0],
                            club_id: 0
                        }), 1 != app.globalData.webset.community_free && 1 != app.globalData.freeshipping && (express_price_2 = app.look.count_express("", number, weight, e.data.club.region));
                    },
                    fail: function() {
                        e.setData({
                            loadend: !0
                        });
                    }
                });
            }
        }) : 1 != app.globalData.webset.community_free && 1 != app.globalData.freeshipping && (express_price_2 = app.look.count_express("", number, weight, e.data.club.region)); else {
            var i, s = this.data.disabled;
            this.data.express_price_1 > this.data.money ? (i = 2, s = !0) : s = !(i = 1), this.setData({
                staus: i,
                disabled: s
            });
        }
    },
    showClubList: function() {
        if (this.setData({
            showClubList: !0
        }), null == this.data.clubList) {
            var e = this;
            wx.getLocation({
                success: function(a) {
                    e.setData({
                        longitude: a.longitude,
                        latitude: a.latitude
                    }), app.util.request({
                        url: "entry/wxapp/community",
                        showLoading: !0,
                        data: {
                            op: "clubList",
                            longitude: a.longitude,
                            latitude: a.latitude,
                            page: e.data.page,
                            pagesize: e.data.pagesize
                        },
                        success: function(a) {
                            for (var t = 0; t < a.data.data.clubList.length; t++) 1e3 < a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = (a.data.data.clubList[t].juli / 1e3).toFixed(1) + "km" : -1 == a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = "未知" : a.data.data.clubList[t].julishow = a.data.data.clubList[t].juli + "m";
                            e.setData({
                                clubList: a.data.data.clubList
                            });
                        },
                        fail: function() {
                            e.setData({
                                loadend: !0
                            });
                        }
                    });
                },
                fail: function(a) {
                    wx.showModal({
                        title: "需要授权使用地理位置",
                        content: "需要授权使用地理位置",
                        success: function(a) {
                            a.confirm ? (e.data.isaddr = !0, wx.openSetting()) : a.cancel && (e.data.longitude = -1, 
                            e.data.latitude = -1, e.data.page = 0, e.loadClubList());
                        }
                    });
                }
            });
        }
    },
    loadClubList: function() {
        if (!this.data.loadend && !this.data.Clubloading) {
            var e = this;
            this.data.Clubloading = !0, e.data.page = e.data.page + 1, app.util.request({
                url: "entry/wxapp/community",
                showLoading: !0,
                data: {
                    op: "clubList",
                    longitude: e.data.longitude,
                    latitude: e.data.latitude,
                    page: e.data.page,
                    pagesize: e.data.pagesize
                },
                success: function(a) {
                    for (var t = 0; t < a.data.data.clubList.length; t++) 1e3 < a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = (a.data.data.clubList[t].juli / 1e3).toFixed(1) + "km" : -1 == a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = "未知" : a.data.data.clubList[t].julishow = a.data.data.clubList[t].juli + "m";
                    e.data.clubList ? e.setData({
                        clubList: e.data.clubList.concat(a.data.data.clubList)
                    }) : e.setData({
                        clubList: a.data.data.clubList
                    });
                },
                complete: function(a) {
                    e.data.Clubloading = !1;
                },
                fail: function(a) {
                    app.look.alert(a.data.message), e.setData({
                        loadend: !0
                    });
                }
            });
        }
    },
    hideClubList: function() {
        this.setData({
            showClubList: !1
        });
    },
    tapClubList: function(a) {
        this.setData({
            club_id: a.currentTarget.dataset.index
        });
    },
    selectClub: function() {
        this.data.club && this.data.club.region == this.data.clubList[this.data.club_id] || 1 != app.globalData.webset.community_free && 1 != app.globalData.freeshipping && (express_price_2 = app.look.count_express("", number, weight, this.data.clubList[this.data.club_id].region)), 
        this.setData({
            club: this.data.clubList[this.data.club_id],
            showClubList: !1
        });
    },
    onLoad: function(a) {
        app.address;
        var e = this, t = 0, i = 0, s = [];
        for (var l in app.globalData.packagedata.chosepro) s.push(app.globalData.packagedata.chosepro[l]);
        for (var l in s) for (var u in s[l].attrs) t++, s[l].weight && 0 == isNaN(parseInt(s[l].weight)) && (i += parseInt(s[l].weight));
        number = t, weight = i;
        var d = 1 == app.globalData.webset.community && 1 == app.globalData.packageconfig.deliverycommunity ? 2 : 1;
        if (-1 != app.globalData.packageconfig.deliveryexpress && 1 == app.globalData.webset.express || -1 != app.globalData.packageconfig.deliverycommunity && 1 == app.globalData.webset.community) {
            if (this.setData({
                money: app.globalData.userInfo.amount,
                webset: app.globalData.webset,
                chosepro: s,
                totalnum: t,
                deliveryexpress: app.globalData.packageconfig.deliveryexpress,
                deliverycommunity: app.globalData.packageconfig.deliverycommunity,
                navIndex: 1 == app.globalData.webset.community && 1 == app.globalData.packageconfig.deliverycommunity ? 2 : 1
            }), 2 == d && (null == this.data.club ? wx.getLocation({
                success: function(a) {
                    e.setData({
                        longitude: a.longitude,
                        latitude: a.latitude
                    }), app.util.request({
                        url: "entry/wxapp/community",
                        showLoading: !0,
                        data: {
                            op: "clubList",
                            longitude: a.longitude,
                            latitude: a.latitude,
                            page: e.data.page,
                            pagesize: e.data.pagesize
                        },
                        success: function(a) {
                            for (var t = 0; t < a.data.data.clubList.length; t++) 1e3 < a.data.data.clubList[t].juli ? a.data.data.clubList[t].julishow = (a.data.data.clubList[t].juli / 1e3).toFixed(1) + "km" : a.data.data.clubList[t].julishow = a.data.data.clubList[t].juli + "m";
                            e.setData({
                                clubList: a.data.data.clubList,
                                club: a.data.data.clubList[0],
                                club_id: a.data.data.clubList[0].id
                            }), 1 != app.globalData.webset.community_free && 1 != app.globalData.freeshipping && (express_price_2 = app.look.count_express("", number, weight, e.data.club.region));
                        },
                        fail: function() {
                            e.setData({
                                loadend: !0
                            });
                        }
                    });
                }
            }) : (1 != app.globalData.webset.community_free && 1 != app.globalData.freeshipping && (express_price_2 = app.look.count_express("", number, weight, e.data.club.region)), 
            e.setData({
                club: app.club,
                express_price_2: express_price_2
            }))), this.data.express_price_2 > this.data.money) {
                this.setData({
                    staus: 2,
                    disabled: !0
                });
            }
        } else d = 0;
    },
    myform: function(a) {
        var t = a.detail.formId, e = a.detail.value.remarks, i = a.detail.value.paytype, s = this.data.address;
        if ("" != s && null != s || 2 == this.data.navIndex && "-1" == app.globalData.webset.community_personal_addr) if ("" != i) if (this.data.isexpress || 2 == this.data.navIndex) {
            if (2 == this.data.navIndex && null == this.data.club) return this.showClubList(), 
            void app.look.alert("当前没有社区团点,请选择团点其他模式");
            var l = {};
            l.address = this.data.address, l.club = this.data.club, l.packagedata = app.globalData.packagedata, 
            l.paytype = i, l.formid = t, l.remarks = e, 2 == this.data.navIndex ? l.is_community = 1 : l.is_community = -1;
            var u = {
                op: "createorder",
                savemodel: JSON.stringify(l)
            };
            app.util.request({
                url: "entry/wxapp/package",
                showLoading: !0,
                method: "POST",
                data: u,
                success: function(a) {
                    var t = a.data.data;
                    if (console.log(t), 1 == t.status) app.globalData.userInfo = t.userinfo, app.util.message({
                        title: "支付成功",
                        redirect: "reLaunch:/xc_xinguwu/fastshop/rechargeAward/rechargeAward"
                    }), console.log(t), console.log(app.globalData.userInfo); else {
                        console.log(t);
                        var e = t.pay_params, i = e.tid;
                        wx.requestPayment({
                            timeStamp: e.timeStamp,
                            nonceStr: e.nonceStr,
                            package: e.package,
                            signType: "MD5",
                            paySign: e.paySign,
                            success: function(a) {
                                setTimeout(function() {
                                    !function a(t) {
                                        app.util.request({
                                            url: "entry/wxapp/payquery",
                                            showLoading: !0,
                                            data: {
                                                tid: t
                                            },
                                            success: function(a) {
                                                app.util.message({
                                                    title: "支付成功",
                                                    redirect: "reLaunch:/xc_xinguwu/fastshop/rechargeAward/rechargeAward"
                                                });
                                            },
                                            fail: function() {
                                                setTimeout(function() {
                                                    a(t);
                                                }, 1e3);
                                            }
                                        });
                                    }(i);
                                }, 500);
                            },
                            fail: function(a) {
                                "requestPayment:fail cancel" === a.errMsg && app.util.message({
                                    title: "未支付",
                                    redirect: "reLaunch:../../fastshop/rechargeAward/rechargeAward?status=x2"
                                });
                            }
                        });
                    }
                }
            });
        } else wx.showToast({
            title: "该地址不配送",
            icon: "none"
        }); else wx.showToast({
            title: "请选择支付方式",
            icon: "none"
        }); else wx.showToast({
            title: "请选择您的地址信息",
            icon: "none"
        });
    },
    onReady: function() {
        app.look.navbar(this), wx.setNavigationBarTitle({
            title: "礼品"
        });
    },
    onShow: function() {
        var a = this, t = app.address;
        null != t && 1 != app.globalData.freeshipping && (express_price_1 = app.look.count_express("", number, weight, t.region), 
        a.setData({
            address: t,
            express_price_1: express_price_1
        })), a.data.isaddr && 1 == a.data.page && (a.data.longitude = -1, a.data.latitude = -1, 
        a.data.page = 0, a.loadClubList());
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {}
});